package avicit.cbb.businessclass.fondsmanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import avicit.cbb.businessclass.fondsmanage.domain.DSConfigBean;
import avicit.cbb.businessclass.fondsmanage.service.CbbFondsDbService;
import avicit.cbb.businessclass.fondsmanage.service.ParseDSConfigService;

@Service
public class ParseDSConfigServiceImpl implements ParseDSConfigService{
	@Autowired(required = false)
	private CbbFondsDbService cbbFondsDbService;
	/**
	 * 构造函数
	 */
	public ParseDSConfigServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 读取xml配置文件
	 * @param path
	 * @return
	 * @throws Exception 
	 *//*
	public Vector readConfigInfo(String path) throws Exception {
		ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
		Resource res = resourceResolver.getResource("classpath:/configuration/"+path);
		String filePath = String.valueOf(res.getURL());
		filePath = filePath.substring(6);
		Vector dsConfig = null;
		FileInputStream fi = null;
		try {
			fi = new FileInputStream(filePath);// 读取路径文件
			dsConfig = new Vector();
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(fi);
			Element root = doc.getRootElement();
			List pools = root.getChildren();
			Element pool = null;
			Iterator allPool = pools.iterator();
			while (allPool.hasNext()) {
				pool = (Element) allPool.next();
				DSConfigBean dscBean = new DSConfigBean();
				dscBean.setType(pool.getChild("type").getText());
				dscBean.setName(pool.getChild("name").getText());
				dscBean.setDriver(pool.getChild("driver").getText());
				dscBean.setUrl(pool.getChild("url").getText());
				dscBean.setUsername(pool.getChild("username").getText());
				dscBean.setPassword(pool.getChild("password").getText());
				//连接池在每个分区中的最大连接数 
				dscBean.setMaxConnPer(Integer.parseInt(pool.getChild("maxConnPer").getText()));
				//连接池在每个分区中的最小连接数
				dscBean.setMinConnPer(Integer.parseInt(pool.getChild("minConnPer").getText()));
				//设置分区 
				dscBean.setPartitionCount(Integer.parseInt(pool.getChild("partitionCount").getText()));
				//设置连接空闲时间(分钟)
				dscBean.setIdleMaxAge(Long.parseLong(pool.getChild("idleMaxAge").getText()));
				//当连接池中的连接耗尽时BoneCP一次同时获取的连接数 
				dscBean.setAcquireIncrement(Integer.parseInt(pool.getChild("acquireIncrement").getText()));
				//设置连接池阀值
				dscBean.setPoolAvailabilityThreshold(Integer.parseInt(pool.getChild("poolAvailabilityThreshold").getText()));
				//获取connection超时的时间
				dscBean.setConnectionTimeout(Long.parseLong(pool.getChild("connectionTimeout").getText()));
				//设置测试connection的间隔时间(分钟)
				dscBean.setIdleConnectionTestPeriod(Long.parseLong(pool.getChild("idleConnectionTestPeriod").getText()));
				dsConfig.add(dscBean);
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				fi.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return dsConfig;
	}

	*//**
	 *增加配置文件
	 * @throws IOException 
	 *
	 *//*
	public void addConfigInfo(String path, DSConfigBean dsb) throws Exception {
		ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
		Resource res = resourceResolver.getResource("classpath:/configuration/"+path);
		String filePath = String.valueOf(res.getURL());
		filePath = filePath.substring(6);
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			fi = new FileInputStream(filePath);// 读取xml流
			SAXBuilder sb = new SAXBuilder();

			Document doc = sb.build(fi); // 得到xml
			Element root = doc.getRootElement();
			List pools = root.getChildren();// 得到xml子树

			Element newpool = new Element("pool"); // 创建新连接池

			Element pooltype = new Element("type"); // 设置连接池类型
			pooltype.setText(dsb.getType());
			newpool.addContent(pooltype);

			Element poolname = new Element("name");// 设置连接池名字
			poolname.setText(dsb.getName());
			newpool.addContent(poolname);

			Element pooldriver = new Element("driver"); // 设置连接池驱动
			pooldriver.addContent(dsb.getDriver());
			newpool.addContent(pooldriver);

			Element poolurl = new Element("url");// 设置连接池url
			poolurl.setText(dsb.getUrl());
			newpool.addContent(poolurl);

			Element poolusername = new Element("username");// 设置连接池用户名
			poolusername.setText(dsb.getUsername());
			newpool.addContent(poolusername);

			Element poolpassword = new Element("password");// 设置连接池密码
			poolpassword.setText(dsb.getPassword());
			newpool.addContent(poolpassword);

			Element poolmaxConnPer = new Element("maxConnPer");// 设置连接池最大连接
			poolmaxConnPer.setText(String.valueOf(dsb.getMaxConnPer()));
			newpool.addContent(poolmaxConnPer);
			
			Element poolminConnPer = new Element("minConnPer");
			poolminConnPer.setText(String.valueOf(dsb.getMinConnPer()));
			newpool.addContent(poolminConnPer);
			
			
			Element poolpartitionCount = new Element("partitionCount");
			poolpartitionCount.setText(String.valueOf(dsb.getPartitionCount()));
			newpool.addContent(poolpartitionCount);
			
			Element poolidleMaxAge = new Element("idleMaxAge");
			poolidleMaxAge.setText(String.valueOf(dsb.getIdleMaxAge()));
			newpool.addContent(poolidleMaxAge);
			
			Element poolacquireIncrement = new Element("acquireIncrement");
			poolacquireIncrement.setText(String.valueOf(dsb.getAcquireIncrement()));
			newpool.addContent(poolacquireIncrement);
			
			Element poolAvailabilityThreshold = new Element("poolAvailabilityThreshold");
			poolAvailabilityThreshold.setText(String.valueOf(dsb.getPoolAvailabilityThreshold()));
			newpool.addContent(poolAvailabilityThreshold);
			
			Element poolconnectionTimeout = new Element("connectionTimeout");
			poolconnectionTimeout.setText(String.valueOf(dsb.getConnectionTimeout()));
			newpool.addContent(poolconnectionTimeout);
			
			Element poolidleConnectionTestPeriod = new Element("idleConnectionTestPeriod");
			poolidleConnectionTestPeriod.setText(String.valueOf(dsb.getIdleConnectionTestPeriod()));
			newpool.addContent(poolidleConnectionTestPeriod);
			
			pools.add(newpool);// 将child添加到root
			Format format = Format.getPrettyFormat();
			format.setIndent("");
			format.setEncoding("utf-8");
			XMLOutputter outp = new XMLOutputter(format);
			fo = new FileOutputStream(filePath);
			outp.output(doc, fo);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
	}

	*//**
	 *删除配置文件
	 * @throws IOException 
	 *//*
	public void delConfigInfo(String path, String name) throws Exception {
		ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
		Resource res = resourceResolver.getResource("classpath:/configuration/"+path);
		String filePath = String.valueOf(res.getURL());
		filePath = filePath.substring(6);
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			fi = new FileInputStream(filePath);// 读取路径文件
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(fi);
			Element root = doc.getRootElement();
			List pools = root.getChildren();
			Element pool = null;
			Iterator allPool = pools.iterator();
			while (allPool.hasNext()) {
				pool = (Element) allPool.next();
				if (pool.getChild("name").getText().equals(name)) {
					pools.remove(pool);
					break;
				}
			}
			Format format = Format.getPrettyFormat();
			format.setIndent("");
			format.setEncoding("utf-8");
			XMLOutputter outp = new XMLOutputter(format);
			fo = new FileOutputStream(filePath);
			outp.output(doc, fo);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				fi.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/
	
	/**
	 * 获取数据源信息
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Vector getDsConfigInfo(Map<String,Object> paramMap) throws Exception {
		List<Map<String,Object>> listConn = new ArrayList<Map<String,Object>>();
		listConn = this.cbbFondsDbService.getCbbFondsDbInfoStr(paramMap);
		Vector dsConfig = new Vector();
		if (listConn != null && listConn.size() > 0){
			Map<String,Object> pool = new HashMap<String,Object>();
			for (int i = 0; i < listConn.size(); i++) {
				pool = listConn.get(i);
				DSConfigBean dscBean = new DSConfigBean();
				dscBean.setType(String.valueOf(pool.get("databaseType")));
				dscBean.setName(String.valueOf(pool.get("cpName")));
				dscBean.setDriver(String.valueOf(pool.get("connDriver")));
				dscBean.setUrl(String.valueOf(pool.get("connStr")));
				dscBean.setUsername(String.valueOf(pool.get("dbUser")));
				dscBean.setPassword(String.valueOf(pool.get("dbPwd")));
				
				//连接池在每个分区中的最大连接数 
				dscBean.setMaxConnPer(Integer.parseInt(String.valueOf(pool.get("maxConnPer"))));
				//连接池在每个分区中的最小连接数
				dscBean.setMinConnPer(Integer.parseInt(String.valueOf(pool.get("minConnPer"))));
				//设置分区 
				dscBean.setPartitionCount(Integer.parseInt(String.valueOf(pool.get("partitionCount"))));
				//设置连接空闲时间(分钟)
				dscBean.setIdleMaxAge(Long.parseLong(String.valueOf(pool.get("idleMaxAge"))));
				//当连接池中的连接耗尽时BoneCP一次同时获取的连接数 
				dscBean.setAcquireIncrement(Integer.parseInt(String.valueOf(pool.get("acquireIncrement"))));
				//设置连接池阀值
				dscBean.setPoolAvailabilityThreshold(Integer.parseInt(String.valueOf(pool.get("poolAvailabilityThreshold"))));
				//获取connection超时的时间
				dscBean.setConnectionTimeout(Long.parseLong(String.valueOf(pool.get("connectionTimeout"))));
				//设置测试connection的间隔时间(分钟)
				dscBean.setIdleConnectionTestPeriod(Long.parseLong(String.valueOf(pool.get("idleConnectionTestPeriod"))));
				dsConfig.add(dscBean);
			}
		}
		return dsConfig;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Vector readConfigInfo(String path) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addConfigInfo(String path, DSConfigBean dsb) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delConfigInfo(String path, String name) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
